package com.cssw.fyzb.core.eao.acct;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.core.eao.video.VideoEao;
import com.cssw.fyzb.core.es.EsClient;
import com.cssw.fyzb.core.model.eos.AcctLogEo;
import com.cssw.fyzb.core.model.eos.VideoEo;
import com.cssw.fyzb.core.util.DateUtil;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.ScriptSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author YangYiQun
 */
@Component
public class AcctLogEaoImpl implements AcctLogEao {

    @Autowired
    private EsClient esClient;

    private static final String INDEX_NAME = "fyzb_acct_log";
    private static final String TYPE_NAME = "fyzb_acct_log";

    @Override
    public void save(String logId, Long acctId, Long userId, String acctType, BigDecimal fee, String feeInDe,
                     String tradeId, String tradeType, String tradeName, String busiType, Date tradeTime) {
        AcctLogEo acctLogEo = new AcctLogEo();
        acctLogEo.setLogId(logId);
        acctLogEo.setUserId(userId);
        acctLogEo.setAcctId(acctId);
        acctLogEo.setAcctType(acctType);
        acctLogEo.setFee(fee);
        acctLogEo.setFeeInDe(feeInDe);
        acctLogEo.setTradeId(tradeId);
        acctLogEo.setTradeType(tradeType);
        acctLogEo.setTradeName(tradeName);
        acctLogEo.setBusiType(busiType);
        acctLogEo.setTradeTime(DateUtil.format(tradeTime));
        acctLogEo.setCreateTime(System.currentTimeMillis());

        esClient.getClient()
                .prepareIndex(INDEX_NAME, TYPE_NAME)
                .setSource(JSON.parseObject(JSON.toJSONString(acctLogEo)))
                .get()
        ;
    }

    @Override
    public Page<AcctLogEo> pageByType(int pageNumber, int pageSize, Long userId, String acctType) {

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.filter().add(QueryBuilders.termQuery("userId", userId));
        queryBuilder.filter().add(QueryBuilders.termQuery("acctType", acctType));

        SearchResponse response = esClient.getClient()
                .prepareSearch(INDEX_NAME)
                .setQuery(queryBuilder)
                .setFrom(pageNumber * (pageSize - 1))
                .setSize(pageSize)
                .addSort(
                        SortBuilders.fieldSort("createTime").order(SortOrder.DESC)
                )
                .get();

        return toPage(response, pageNumber, pageSize);
    }


    private Page<AcctLogEo> toPage(SearchResponse response, int pageNumber, int pageSize) {
        Page<AcctLogEo> page = new Page<>();
        int count = (int) response.getHits().totalHits;
        page.setTotalPage((int) Math.ceil(count * 1.0 / pageSize));
        page.setTotalRow(count);
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        List<AcctLogEo> list = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            list.add(JSON.parseObject(JSON.toJSONString(hit.getSourceAsMap()), AcctLogEo.class));
        }
        page.setList(list);
        return page;
    }
}
